#CROSS_PATH=mips-linux-gnu-
#CROSS=$(CROSS_PATH)
#CC=$(CROSS)gcc -mips32r2 -msoft-float -muclibc
AR=$(CROSS)ar
LD=$(CROSS)ld
STRIP=$(CROSS)strip
RANLIB=$(CROSS)ranlib
SIZE=$(CROSS)size
#LIBDIR=$(CROSS_PATH)/lib
PWD = $(shell pwd)
CURRENT_OMCI_DIR = $(PWD)/../..

##################################
########Configured output#########
##################################
LINK_NAME	=	libapi_omci_adpt.so
SONAME		=	$(LINK_NAME).1
REAL_NAME	=	$(SONAME).0.0
RM				= /bin/rm -f
LN      		= /bin/ln -fs
###################################

INCLUDE = -I$(INSTALL_KERNELHEADER) \
		-I$(CURRENT_OMCI_DIR)/inc/omci_adpt \
		-I$(CURRENT_OMCI_DIR)/inc/omci_adpt/omci_me \
		-I$(CURRENT_OMCI_DIR)/inc/omci_lib \
		-I$(XPON_OMCI_FRAME_INC_DIR) \
		-I$(CURRENT_OMCI_DIR)/inc/gpon_flow_lib \
		-I$(INSTALL_BSP_GLOBAL_INC_XPON_PRIVATE_DIR) \
		-I$(INSTALL_BSP_GLOBAL_INC_XPON_PUBLCI_DIR) \
		$(LIBINSTALL_CFLAGS) \
		$(BSP_EXT_CFLAGS) -I$(LIB_DIR_XPON) 

ifeq ($(strip $(XPON_BSP)),)		
INCLUDE += -I$(APP_CFG_NG_DIR)/hal_itf \
			-I$(APP_CFG_NG_DIR)/service/cfg 
else
INCLUDE += $(BSPAPP_CFLAG0) 
endif

OMCI_ME_DIR := omci_me
OBJ_DIR := $(PWD)/.tmp

ifneq ($(strip $(TCSUPPORT_CODE_SHRINK)),)
CFLAGS += -Os
else
CFLAGS += -O2
endif
CFLAGS += -Wall $(INCLUDE)

CFLAGS += $(TC_CFLAGS) $(BSP_CFLAGS)  -D _GNU_SOURCE 

CFLAGS += -Werror

ifneq ($(strip $(TCSUPPORT_OMCI_CTC) \
				$(TCSUPPORT_CUC) \
				$(TCSUPPORT_OMCI_ALCATEL) \
				$(TCSUPPORT_UPSTREAM_VLAN_POLICER) \
				$(TCSUPPORT_VNPTT) \
				$(TCSUPPORT_HUAWEI_OLT_VENDOR_SPECIFIC_ME) \
				$(TCSUPPORT_HUAWEI_OLT_VENDOR_SPECIFIC_ME_FOR_INA) \
				$(TCSUPPORT_CHS)),)
CFLAGS += -DSUPPORT_PRIVATE_OMCI
SUPPORT_PRIVATE_OMCI = 1
endif

OMCI_MEM_CHECK =
ifneq ($(strip $(OMCI_MEM_CHECK)),)
CFLAGS+=-DMEMORY_DEBUG
endif


LIBS = -lm -lcrypt $(BSP_EXT_LDFLAGS) -lpthread -lrt 
LIBS += -L $(LIB_DIR_XPON)  -lxpon -lapi_lib_switchmgr -lubus -lblobmsg_json -ljson-c -lubox
LIBS += -L $(LIB_DIR_XPON)  -lxpon -lapi_lib_fe

ifneq ($(strip $(XPON_BSP)),)
LIBS += $(BSPAPP_LDFLAG0) 
endif

ifneq ($(strip $(TCSUPPORT_OPENWRT)),)
LIBS +=  -luci
endif

LIBS += $(COMPILEOPTION_LIB)

ifeq ($(strip $(XPON_BSP)),)	
LIBS += -L $(CFG_NG_LIB_DIR) -lhal_itf_wan

ifneq ($(strip $(TCSUPPORT_TCAPI_ENHANCE)),)
ifneq ($(strip $(TCSUPPORT_TCAPI_ENHANCE_GPON)),)
LIBS += $(LIBINSTALL_LDFLAGS) -ltcapi_enhance
else
LIBS += $(LIBINSTALL_LDFLAGS) $(TCAPI_LIB)
endif
else
LIBS += $(LIBINSTALL_LDFLAGS) $(TCAPI_LIB)
endif

else
CFLAGS+= -DBSP_BUILD
endif

MT7530_SWITCH_API = $(BSP_EXT_LDFLAGS) -lmtkswitch
LIBS += $(MT7530_SWITCH_API)

ifneq ($(strip $(TCSUPPORT_OPENWRT)),)
INCLUDE += -I$(XPON_APP_LIB_INSTALL_DIR)
LIBS += -L$(XPON_APP_LIB_INSTALL_DIR)
endif

LIBS += -lblapi_traffic -lblapi_system

ALL_SRC = omci_adapter.c
ifeq ($(XPON_BSP),)
ALL_SRC += omci_adpt_sdk.c
else
ALL_SRC += omci_adpt_bsp.c
endif
ALL_SRC += omci_adpt_gpon_flow.c
ALL_SRC += omci_upgrade_func.c

OMCI_ME_SRC = omci_me_ani_management_func.c
OMCI_ME_SRC += omci_me_enhanced_security_ctl_func.c
OMCI_ME_SRC += omci_me_equipment_management_func.c
OMCI_ME_SRC += omci_me_ethernet_services_func.c
OMCI_ME_SRC += omci_me_gemport_mapping_func.c 
OMCI_ME_SRC += omci_me_general_purpose_func.c 
OMCI_ME_SRC += omci_me_layer2_data_services_func.c
OMCI_ME_SRC += omci_me_layer3_data_services_func.c 
OMCI_ME_SRC += omci_me_mac_bridge_func.c 
OMCI_ME_SRC += omci_me_multicast_func.c
OMCI_ME_SRC += omci_me_traffic_management_func.c 
OMCI_ME_SRC += omci_me_vlan_func.c 
OMCI_ME_SRC += omci_me_voip_func.c


ifneq ($(strip $(TCSUPPORT_OMCI_CTC)),)
OMCI_ME_SRC += omci_me_ctc_func.c
OMCI_ME_SRC += omci_me_ctc_nodetable.c
endif
ifneq ($(strip $(TCSUPPORT_OMCI_ALCATEL)),)
OMCI_ME_SRC += omci_me_alcatel_func.c
OMCI_ME_SRC += omci_me_alcatel_nodetable.c
endif
ifneq ($(strip $(SUPPORT_PRIVATE_OMCI)),)
OMCI_ME_SRC += omci_me_private_func.c
OMCI_ME_SRC += omci_me_private_nodetable.c
endif

ALL_SRC += $(OMCI_ME_SRC:%=$(OMCI_ME_DIR)/%)

OBJS = $(ALL_SRC:.c=.o)

ifneq ($(strip $(XPON_BSP)),)	
RPATH_LINK_DIR=$(LIB_DIR_XPON):$(BSP_EXT_LIB)
else
RPATH_LINK_DIR=$(LIB_DIR_XPON)
endif

TARGET = $(REAL_NAME)

%.o: %.c
	@echo "***********" compiling $<
	$(CC) $(CFLAGS) -fPIC -c -o $@ $< 

all: $(TARGET) install

ifneq ($(MAKECMDGOALS), clean)
# dependency files, one for each *.c, named as *.d
# don't panic, the recipe is copied directly from GNU Make's official doc
%.d: %.c
	@echo ***********generating dependency file for $<
	@set -e; rm -f $@; \
	$(CC) $(CFLAGS) $(INCLUDE) -MM $< |\
        sed "s?\\(.*\\):?$(basename $<).o $(basename $<).d :?g" \
        > $@ || rm -rf $@ 	

-include $(ALL_SRC:.c=.d)
endif

$(TARGET): $(OBJS)
	@echo "***********" generating executable: $@
	@$(CC) -Wall -Wl,-rpath-link,$(RPATH_LINK_DIR) -fPIC -shared -o  $(TARGET) $(OBJS) $(LIBS)
	@$(STRIP) $(TARGET)

install:
	$(LN) $(REAL_NAME) $(LINK_NAME)
	$(LN) $(REAL_NAME) $(SONAME)
	cp -a $(LINK_NAME)* $(XPON_APP_LIB_INSTALL_DIR)

clean:
	@rm -f *.o *.d $(OMCI_ME_DIR)/*.o $(OMCI_ME_DIR)/*.d $(TARGET)
	$(RM) $(FILESYSTEM_DIR)/lib/$(LINK_NAME)*		
	@echo clean done!
